<?php
/**
 * Created by PhpStorm.
 * User: 黑盒子
 * Date: 2020/11/16
 * Time: 23:14
 */

namespace app\api\controller;

use app\common\exception\ApiException;

class Common
{
    /**
     * 文件上传
     */
    public function upload(){
        $file = request()->file('file');
        $file_url = '';

        if($file){
            $static_path = '/static/files/'.date('Ymd');
            $file_path = \app()->getRootPath() . 'public' . $static_path;
            $info = $file->rule('uniqid')->move($file_path);
            if ($info) {//上传成功
                $file_name = $info->getFilename();
//                $file_url = Config('app_host') . $static_path . $file_name;
                $file_url = $static_path . '/' . $file_name;
            }
        }

        if (!$file_url) {
            throw new ApiException(['msg' => '上传失败']);
        }

        return json(['success' => 1, 'file_url' => $file_url]);
    }
}